package com.qs.home.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.databinding.Observable;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.qs.base.base.BaseMyActivity;
import com.qs.base.router.RouterActivityPath;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeQrResultActivityBinding;
import com.qs.home.ui.VM.QRResultViewModel;

import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 二维码扫描结果
 *
 * @Author ltzz
 * @Date 2019/8/23
 */
@Route(path = RouterActivityPath.Home.QR_RESULT_ACTIVITY)
public class QRResultActivity extends BaseMyActivity<HomeQrResultActivityBinding, QRResultViewModel> {

    @Autowired()
    String qrResult;

    @Override
    protected boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.home_qr_result_activity;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.head.setTitleLeftText("扫描结果");
        binding.head.setAutoFinish(this);
        if (!StringUtils.isTrimEmpty(qrResult))
            viewModel.qrResult.set(qrResult);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //复制
        viewModel.uc.copyTextUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", viewModel.qrResult.get());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtils.showShort("文本已复制");
            }
        });
    }
}
